﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Framework.Frame.Architecture;
using Resto.CostControl.Core.Query.MaterialModule.ViewObject;
using Resto.CostControl.Core.Domain.Model;
using Enterprise.Framework.DomainModel.Repositories;
using Enterprise.Framework.DomainModel.Specifications;
using Enterprise.Framework.DomainModel;

namespace Resto.CostControl.Core.Query.MaterialModule.QueryObject
{
    public class MaterialQO : IApplicationQueryObject
    {
        private IQueryable<MaterialVO> GetMaterialVOSet()
        {
            IEntityRepository<Material> repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<Material>();

            IQueryable<MaterialVO> list = from o in repository.Objects
                                          where o.IsDelete == false
                                          select new MaterialVO()
                                         {
                                             ID = o.ID,
                                             Code = o.Code,
                                             Name = o.Name,
                                             Specification = o.Specification,
                                             ActualSorage = o.ActualStorage,
                                             CurrentPrice = o.CurrentPrice,
                                             SafeStorage = o.SafeStorage,
                                             TypeCode = o.Type,
                                             Unit = o.Unit,
                                             CategoryCode = o.Category,
                                         };

            return list;
        }

        public MaterialVO[] QueryMaterialVOByPager(ISpecification<MaterialVO> spec, PagerCondition pager, string orderKey, bool isAscending)
        {
            IQueryable<MaterialVO> list = GetMaterialVOSet();

            if (spec != null)
                list = list.Where(spec.GetExpression());

            IEnumerable<MaterialVO> list2 = RepositoryExtention.Query(list, pager, orderKey, isAscending);
            return list2.ToArray();
        }

        public MaterialVO[] QueryAllMaterialVO()
        {
            IQueryable<MaterialVO> list = GetMaterialVOSet();

            return list.ToArray();
        }

    }
}
